#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *str_repeat_removal(const char *source, char * dest)
{
    // 根据位图 去重 的代码实现
    int i = 0, j = 0;
    int cnt = 0;
    char *temp;
    int len;
    unsigned int bitmap[8] = {0,0,0,0,0,0,0,0};
    unsigned char c;
    unsigned int mask;

    if(!source || !dest) {  return NULL;  }

    temp = (char*)malloc(strlen(source) + 1);
    if(!temp)   {  return NULL;  }
    memset(temp, 0, strlen(temp));

    while(source[i])
    {
        c = source[i];
        mask = 1 << (c % 32);
        if ((bitmap[c >> 5] & mask) == 0)  
        {
            temp[j++] = source[i];
            bitmap[c >> 5] |= mask;
        }
        i++;
    }

    temp[j+1] = '\0';

    sprintf(dest, "%s", temp);
    len = strlen(dest);

    // printf("len = %d\n",len);

    free(temp);
    return dest;
}
#if 1
 // for demo

int test1(void)
{
    char source[7] = "keep12";  
    char dest[1] = {0};  
   
    printf("\n\nTest 1 \n\n");
    printf("Before %s\n", source);
    str_repeat_removal(source, dest);
    printf("After  %s\n", dest);
}

int test2(void)
{

    char *source;  
    char dest[1] = {0};  
   
    printf("\n\nTest 2 \n\n");
    printf("Before %s\n",source);
    str_repeat_removal(NULL,NULL);
    printf("After  %s\n", dest);
}

int main()
{
    test1();
    test2();
}
#endif
